package com.shop2cn.iapi.sqdistributor.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.DistributorApplyReq;
import com.shop2cn.iapi.sqdistributor.service.DistributorApplyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.sqdistributor.DistributorApply;
import com.ymttest.database.model.sqdistributor.DistributorRelation;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorApplyWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorRelationWrapper;
import com.ymttest.utils.TestDataManager;

public class Ts_DistributorApply {
	
	//java接口
	private static DistributorApplyReq distributorApplyReq;
	private static DistributorApplyCall distributorApplyCall;
	
	private static DistributorApplyWrapper distributorApplyWrapper = new DistributorApplyWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("申请成为分销员");
	}

	@Before
	public void caseUp() {
		distributorApplyReq = new DistributorApplyReq();
		distributorApplyCall = new DistributorApplyCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(distributorApplyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb(Boolean autoAudit) throws Exception {
		
		DistributorRelationWrapper distributorRelationWrapper = new DistributorRelationWrapper();

		DistributorRelation inviterRelation = distributorRelationWrapper
				.selectByMerchantIdAndDistributorId(
						distributorApplyReq.getMerchantId(),
						distributorApplyReq.getInviterId());

		Integer inviterType = inviterRelation.getDistributor_type();

		DistributorApply actualResult = distributorApplyWrapper
				.selectByApplyUserIdAndReferrerId(
						distributorApplyReq.getDistributorId(),
						distributorApplyReq.getInviterId());

		Logger.verifyEquals(distributorApplyReq.getMerchantId(),
				actualResult.getMerchant_id(), "验证Merchant_id");
		Logger.verifyEquals(distributorApplyReq.getDistributorId(),
				actualResult.getApply_user_id(), "验证Apply_user_id");
		Logger.verifyEquals(
				String.valueOf(distributorApplyReq.getDistributorId()),
				actualResult.getApply_user_name(), "验证Apply_user_name");
		Logger.verifyEquals(distributorApplyReq.getDistributorPhone(),
				actualResult.getApply_user_phone(), "验证Apply_user_phone");
		Logger.verifyEquals(distributorApplyReq.getInviterId(),
				actualResult.getReferrer_id(), "验证Referrer_id");
		Logger.verifyEquals(distributorApplyReq.getInviteCode(),
				actualResult.getInvite_code(), "验证Invite_code");

		if (inviterType == 1) {
			Logger.verifyEquals(1001, actualResult.getDistributor_type(),
					"验证DistributorType");
		} else if (inviterType == 2) {
			Logger.verifyEquals(2001, actualResult.getDistributor_type(),
					"验证DistributorType");
		} else
			Logger.verifyEquals(
					distributorApplyReq.getDistributorType() == null ? 0
							: distributorApplyReq.getDistributorType(),
					actualResult.getDistributor_type(), "验证DistributorType");

		if (autoAudit) {

			DistributorRelation distributorRelation = distributorRelationWrapper
					.selectByMerchantIdAndDistributorId(
							distributorApplyReq.getMerchantId(),
							distributorApplyReq.getDistributorId());

			Logger.verifyEquals(distributorRelation.getDistributor_name(),
					actualResult.getApply_user_name(), "验证Referrer_name");
			Logger.verifyEquals(1, actualResult.getAudit_status(),
					"验证Audit_status");

			if (inviterType == 1) {
				Logger.verifyEquals(1001, actualResult.getDistributor_type(),
						"验证DistributorType");
			} else if (inviterType == 2) {
				Logger.verifyEquals(2001, actualResult.getDistributor_type(),
						"验证DistributorType");
			} else
				Logger.verifyEquals(
						distributorApplyReq.getDistributorType() == null ? 0
								: distributorApplyReq.getDistributorType(),
						actualResult.getDistributor_type(), "验证DistributorType");
		} else
			Logger.verifyEquals(0, actualResult.getAudit_status(),
					"验证Audit_status");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorApply_001() {
		Logger.start(true, "申请成为分销员成功,邀请者为店主，审核模式为手动");
		
		try {
			
			distributorApplyReq.setDistributorId((long)TestDataManager.getRandomIntNum(8));
			distributorApplyReq.setInviteCode(TestDataManager.randomStr(8));
			distributorApplyReq.setDistributorPhone(TestDataManager.getPhoneNum());
			distributorApplyReq.setMerchantId(500861049L);
			distributorApplyReq.setInviterId(500861049L);
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			Logger.verifyEquals(true, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals("success", distributorApplyCall.getMsg(), "验证Msg");
			
			verifyDb(false);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorApply_002() {
		Logger.start(true, "推荐人不存在，申请失败");
		
		try {
			
			distributorApplyReq.setDistributorId((long)TestDataManager.getRandomIntNum(10));
			distributorApplyReq.setInviteCode(TestDataManager.getChineseName());
			distributorApplyReq.setDistributorPhone(TestDataManager.getPhoneNum());
			distributorApplyReq.setMerchantId(500861049L);
			distributorApplyReq.setInviterId((long)TestDataManager.getRandomIntNum(6));
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			Logger.verifyEquals(false, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals(
					String.format("推荐人%s不存在！",
							distributorApplyReq.getInviterId()),
					distributorApplyCall.getMsg(), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorApply_003() {
		Logger.start(true, "重复申请分销员失败");
		
		try {
			
			distributorApplyReq.setDistributorId((long)TestDataManager.getRandomIntNum(10));
			distributorApplyReq.setInviteCode(TestDataManager.randomStr(8));
			distributorApplyReq.setDistributorPhone(TestDataManager.getPhoneNum());
			distributorApplyReq.setMerchantId(500146001L);
			distributorApplyReq.setInviterId(60803066L);
			distributorApplyReq.setDistributorType(0);
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			distributorApplyCall.callService();
			
			Logger.verifyEquals(false, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals(String.format("商户%s与分销员%s的关系已经绑定。",
					distributorApplyReq.getMerchantId(),
					distributorApplyReq.getDistributorId()),
					distributorApplyCall.getMsg(), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorApply_004() {
		Logger.start(true, "ApplyUserId为null， 申请失败");
		
		try {
			
			//distributorApplyReq.setApplyUserId((long)TestDataManager.getRandomIntNum(10));
			distributorApplyReq.setInviteCode(TestDataManager.getChineseName());
			distributorApplyReq.setDistributorPhone(TestDataManager.getPhoneNum());
			distributorApplyReq.setMerchantId(500861041L);
			distributorApplyReq.setInviterId(60792707L);
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			Logger.verifyEquals(false, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals(2, distributorApplyCall.getErrorCode(), "验证code");
			Logger.verifyEquals("distributorId:must not be null", distributorApplyCall.getMsg(), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorApply_005() {
		Logger.start(true, "InviteCode为null， 申请失败");
		
		try {
			
			distributorApplyReq.setDistributorId((long)TestDataManager.getRandomIntNum(10));
			//distributorApplyReq.setInviteCode(TestDataManager.getChineseName());
			distributorApplyReq.setDistributorPhone(TestDataManager.getPhoneNum());
			distributorApplyReq.setMerchantId(500861041L);
			distributorApplyReq.setInviterId(60792707L);
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			Logger.verifyEquals(false, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals(2, distributorApplyCall.getErrorCode(), "验证code");
			Logger.verifyEquals("inviteCode:must not be null", distributorApplyCall.getMsg(), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorApply_006() {
		Logger.start(true, "ApplyUserPhone为null， 申请失败");
		
		try {
			
			distributorApplyReq.setDistributorId((long)TestDataManager.getRandomIntNum(10));
			distributorApplyReq.setInviteCode(TestDataManager.getChineseName());
			//distributorApplyReq.setApplyUserPhone(TestDataManager.getPhoneNum());
			distributorApplyReq.setMerchantId(500861041L);
			distributorApplyReq.setInviterId(60792707L);
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			Logger.verifyEquals(false, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals(2, distributorApplyCall.getErrorCode(), "验证code");
			Logger.verifyEquals("distributorPhone:must not be empty", distributorApplyCall.getMsg(), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorApply_007() {
		Logger.start(true, "ShopId为null， 申请失败");
		
		try {
			
			distributorApplyReq.setDistributorId((long)TestDataManager.getRandomIntNum(10));
			distributorApplyReq.setInviteCode(TestDataManager.getChineseName());
			distributorApplyReq.setDistributorPhone(TestDataManager.getPhoneNum());
			//distributorApplyReq.ShopId(500861041L);
			distributorApplyReq.setInviterId(60792707L);
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			Logger.verifyEquals(false, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals(2, distributorApplyCall.getErrorCode(), "验证code");
			Logger.verifyEquals("merchantId:must not be null", distributorApplyCall.getMsg(), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorApply_008() {
		Logger.start(true, "InviterId为null， 申请失败");
		
		try {
			
			distributorApplyReq.setDistributorId((long)TestDataManager.getRandomIntNum(10));
			distributorApplyReq.setInviteCode(TestDataManager.getChineseName());
			distributorApplyReq.setDistributorPhone(TestDataManager.getPhoneNum());
			distributorApplyReq.setMerchantId(500861041L);
			//distributorApplyReq.setInviterId(60792707L);
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			Logger.verifyEquals(false, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals(1, distributorApplyCall.getErrorCode(), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorApply_009() {
		Logger.start(true, "已经成为分销员的申请人，再次提交分销员申请，同一个商户，申请失败");
		
		try {
			
			distributorApplyReq.setDistributorId(501127906L);
			distributorApplyReq.setInviteCode("龙宗");
			distributorApplyReq.setDistributorPhone("13018473054");
			distributorApplyReq.setMerchantId(501127904L);
			distributorApplyReq.setInviterId(501127906L);
			distributorApplyReq.setDistributorType(0);
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			Logger.verifyEquals(false, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals("分销员ID不能和邀请人ID相同！", distributorApplyCall.getMsg(), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorApply_011() {
		Logger.start(true, "申请成为分销员成功,邀请者为已经存在的分销员，审核模式为手动");
		
		try {
			
			distributorApplyReq.setDistributorId((long)TestDataManager.getRandomIntNum(8));
			distributorApplyReq.setInviteCode(TestDataManager.randomStr(8));
			distributorApplyReq.setDistributorPhone(TestDataManager.getPhoneNum());
			distributorApplyReq.setMerchantId(500924577L);
			distributorApplyReq.setInviterId(25702965L);
			distributorApplyReq.setDistributorType(0);
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			Logger.verifyEquals(true, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals("success", distributorApplyCall.getMsg(), "验证Msg");
			
			verifyDb(false);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorApply_012() {
		Logger.start(true, "申请成为分销员成功,邀请者为店主，审核模式为自动");
		
		try {
			
			distributorApplyReq.setDistributorId((long)TestDataManager.getRandomIntNum(8));
			distributorApplyReq.setInviteCode(TestDataManager.randomStr(8));
			distributorApplyReq.setDistributorPhone(TestDataManager.getPhoneNum());
			distributorApplyReq.setMerchantId(500146001L);
			distributorApplyReq.setInviterId(500146001L);
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			Logger.verifyEquals(true, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals("success", distributorApplyCall.getMsg(), "验证Msg");
			
			verifyDb(true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorApply_013() {
		Logger.start(true, "申请成为分销机构成功,邀请者为店主，审核模式为自动");
		
		try {
			
			distributorApplyReq.setDistributorId((long)TestDataManager.getRandomIntNum(8));
			distributorApplyReq.setInviteCode(TestDataManager.randomStr(8));
			distributorApplyReq.setDistributorPhone(TestDataManager.getPhoneNum());
			distributorApplyReq.setMerchantId(500146001L);
			distributorApplyReq.setInviterId(500146001L);
			distributorApplyReq.setDistributorType(1);
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			Logger.verifyEquals(true, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals("success", distributorApplyCall.getMsg(), "验证Msg");
			
			verifyDb(true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_DistributorApply_014() {
		Logger.start(true, "申请成为CPS成功,邀请者为店主，审核模式为自动");
		
		try {
			
			distributorApplyReq.setDistributorId((long)TestDataManager.getRandomIntNum(8));
			distributorApplyReq.setInviteCode(TestDataManager.randomStr(8));
			distributorApplyReq.setDistributorPhone(TestDataManager.getPhoneNum());
			distributorApplyReq.setMerchantId(500146001L);
			distributorApplyReq.setInviterId(500146001L);
			distributorApplyReq.setDistributorType(2);
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			Logger.verifyEquals(true, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals("success", distributorApplyCall.getMsg(), "验证Msg");
			
			verifyDb(true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorApply_015() {
		Logger.start(true, "申请成为分销机构下级分销员,邀请者为分销机构，DistributorType为1，申请失败");
		
		try {
			
			distributorApplyReq.setDistributorId((long)TestDataManager.getRandomIntNum(8));
			distributorApplyReq.setInviteCode(TestDataManager.randomStr(8));
			distributorApplyReq.setDistributorPhone(TestDataManager.getPhoneNum());
			distributorApplyReq.setMerchantId(500146001L);
			distributorApplyReq.setInviterId(68149049L);
			distributorApplyReq.setDistributorType(1);
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			Logger.verifyEquals(false, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals(3, distributorApplyCall.getErrorCode(), "验证code");
			Logger.verifyEquals("非顶级分销员不能绑定分销机构", distributorApplyCall.getMsg(), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_DistributorApply_016() {
		Logger.start(true, "申请成为CPS下级分销员,邀请者为CPS，DistributorType为2，申请失败");
		
		try {
			
			distributorApplyReq.setDistributorId((long)TestDataManager.getRandomIntNum(8));
			distributorApplyReq.setInviteCode(TestDataManager.randomStr(8));
			distributorApplyReq.setDistributorPhone(TestDataManager.getPhoneNum());
			distributorApplyReq.setMerchantId(500146001L);
			distributorApplyReq.setInviterId(68149049L);
			distributorApplyReq.setDistributorType(2);
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			Logger.verifyEquals(false, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals(3, distributorApplyCall.getErrorCode(), "验证code");
			Logger.verifyEquals("普通分销员只能绑定普通分销员", distributorApplyCall.getMsg(), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DistributorApply_017() {
		Logger.start(true, "申请成为分销机构下级分销员,邀请者为分销机构，申请成功");
		
		try {
			
			distributorApplyReq.setDistributorId((long)TestDataManager.getRandomIntNum(8));
			distributorApplyReq.setInviteCode(TestDataManager.randomStr(8));
			distributorApplyReq.setDistributorPhone(TestDataManager.getPhoneNum());
			distributorApplyReq.setMerchantId(500146001L);
			distributorApplyReq.setInviterId(68149049L);
			distributorApplyReq.setDistributorType(0);
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			Logger.verifyEquals(true, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals("success", distributorApplyCall.getMsg(), "验证Msg");
			
			verifyDb(true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_DistributorApply_018() {
		Logger.start(true, "申请成为CPS下级分销员,邀请者为CPS，DistributorType为2，申请失败");
		
		try {
			
			distributorApplyReq.setDistributorId((long)TestDataManager.getRandomIntNum(8));
			distributorApplyReq.setInviteCode(TestDataManager.randomStr(8));
			distributorApplyReq.setDistributorPhone(TestDataManager.getPhoneNum());
			distributorApplyReq.setMerchantId(500146001L);
			distributorApplyReq.setInviterId(35125905L);
			distributorApplyReq.setDistributorType(0);
			
			distributorApplyCall.setData(distributorApplyReq);
			distributorApplyCall.callService();
			
			Logger.verifyEquals(true, distributorApplyCall.getSuccess(), "验证Success");
			Logger.verifyEquals("success", distributorApplyCall.getMsg(), "验证Msg");
			
			verifyDb(true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
